package com.relytosoft.mizar.media.system.services.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.system.api.entity.SysSecurityLevel;
import com.relytosoft.mizar.media.system.services.mapper.SysSecurityLevelMapper;
import com.relytosoft.mizar.media.system.services.service.SysSecurityLevelService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * SysSecurityLevelServiceImpl
 *
 * @Author SunLi
 * @Date: 2022/4/6 13:45
 * @Version 1.0
 */
@Service
public class SysSecurityLevelServiceImpl extends ServiceImpl<SysSecurityLevelMapper, SysSecurityLevel> implements SysSecurityLevelService {
    @Override
    public int updateSysSecurityLevelStatus(int id,int status){
        return baseMapper.update(null,new LambdaUpdateWrapper<SysSecurityLevel>().set(SysSecurityLevel::getStatus,status).set(SysSecurityLevel::getUpdatedId, CurrentUser.getCurrentUserId()).set(SysSecurityLevel::getUpdatedBy, CurrentUser.getCurrentUserFullName()).set(SysSecurityLevel::getUpdatedTime, LocalDateTime.now()).eq(SysSecurityLevel::getId,id));
    }

    @Override
    public IPage<SysSecurityLevel> selectSysSecurityLevelPageList(PageQuery pageQuery, SysSecurityLevel sysSecurityLevel) {
        LambdaQueryWrapper<SysSecurityLevel> wrapper = Wrappers.lambdaQuery();
        if (!StrUtil.isEmptyIfStr(sysSecurityLevel.getName())) {
            wrapper.eq(SysSecurityLevel::getName, sysSecurityLevel.getName());
        }
        if (ObjectUtil.isNotNull(sysSecurityLevel.getStatus())) {
            wrapper.eq(SysSecurityLevel::getStatus, sysSecurityLevel.getStatus());
        }
        if(StrUtil.isBlank(pageQuery.getOrderByColumn())) {
            pageQuery.setOrderByColumn("id");
            pageQuery.setIsAsc("desc");
        }
        return this.page(pageQuery.build(), wrapper);
    }

    @Override
    public boolean checkSecurityLevelNameUnique(SysSecurityLevel sysSecurityLevel) {
        int id = ObjectUtil.isNull(sysSecurityLevel.getId()) ? -1 : sysSecurityLevel.getId();
        long count = count(new LambdaQueryWrapper<SysSecurityLevel>()
                .eq(SysSecurityLevel::getName, sysSecurityLevel.getName())
                .ne(SysSecurityLevel::getId, id));
        return count <= 0;
    }

}
